'use strict';

const BaseController = require('./base');

class ReplyController extends BaseController {
  get _service() {
    return 'reply';
  }

  // 修改状态
  async _editStatus(status) {
    const { ctx } = this;
    const { id } = ctx.params();
    const result = await this._getService().update({ status, id });
    if (result) {
      this.success();
    } else {
      this.error('更新回复失败');
    }
  }

  async create() {
    const { ctx } = this;
    const { content, houseId, cid, rid } = ctx.params();
    const result = await this._getService().create({ content, houseId, cid, rid, status: 1, uid: ctx.userId });
    if (result) {
      this.success();
    } else {
      this.error('创建回复失败');
    }
  }

  // 更新回复状态
  async edit() {
    const { ctx } = this;
    const { status } = ctx.params();
    const { id } = ctx.params;
    const result = await this._getService().update({ status, id });
    if (result) {
      this.success();
    } else {
      this.error('更新回复失败');
    }
  }

  async agree() {
    await this._editStatus(2);
  }

  async disagree() {
    await this._editStatus(3);
  }

  async destroy() {
    await this._destory('删除论坛失败');
  }

  async show() {
    await this._show('获取论坛失败');
  }

  // 获取房屋下的回复
  async indexByHouseId() {
    const { ctx } = this;
    const { pageIndex, pageSize } = ctx.params();
    const { houseId } = ctx.params;
    const result = await this._getService().findList({ houseId, status: 2, replyStatus: 2, pageIndex, pageSize });
    if (result) {
      this.success(result.data, result.count);
    } else {
      this.error('获取回复失败');
    }
  }

  // 获取论坛下的回复
  async indexByCommentId() {
    const { ctx } = this;
    const { pageIndex, pageSize } = ctx.params();
    const { cid } = ctx.params;
    const result = await this._getService().findList({ cid, status: 2, replyStatus: 2, pageIndex, pageSize });
    if (result) {
      this.success(result.data, result.count);
    } else {
      this.error('获取回复失败');
    }
  }

  // 获取所有回复
  async index() {
    const { ctx } = this;
    const { status, prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;
    const tempResult = await this._getService().findList({ status, prop, sort, pageIndex, pageSize, createdAt });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取回复失败');
    }
  }

  // 获取登录人的回复
  async indexMy() {
    const { ctx } = this;
    const { status, prop, sort, pageIndex, pageSize } = ctx.params();
    const tempResult = await this._getService().findList({ status, uid: ctx.userId, prop, sort, pageIndex, pageSize });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取回复失败');
    }
  }

  // 获取待审核的回复
  async indexExamine() {
    const { ctx } = this;
    const { prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;
    const tempResult = await this._getService().findList({ status: 1, prop, sort, pageIndex, pageSize, createdAt });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取回复失败');
    }
  }
}

module.exports = ReplyController;
